package example;
//可以复制任何类型的文件
import java.io.*;
import java.util.Scanner;

public class Example2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要复制的源文件");
        String srcPath = scanner.next();
        System.out.println("请输入要复制的目标文件");
        String destPath = scanner.next();
        //合法性判定
        //1. 源文件是否存在
        File srcFile = new File(srcPath);
        if(!srcFile.isFile()){
            System.out.println("源文件不存在");
            return;
        }
        //2. 不要求destFile存在,但要求父目录存在
        File destFile = new File(destPath);
        if(!destFile.getParentFile().isDirectory()){
            System.out.println("目标路径有误");
            return;
        }
        //复制
        try(InputStream inputStream = new FileInputStream(srcFile);
            OutputStream outputStream = new FileOutputStream(destFile)) {
            while (true){
                byte[] buffer = new byte[1024];
                int n = inputStream.read(buffer);
                if(n == -1){
                    //说明读取完毕
                    break;
                }
                //把读到的内容写到OutputStream中
                outputStream.write(buffer,0,n);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
